import axios from 'axios'

 /** http request 拦截器 */
axios.interceptors.request.use(
  config => {
    /** 取出token放到头里面 */
    config.headers.Authorization = 'eee'
    return config
  },
  err => {
    return Promise.reject(err)
  })

/** http response 拦截器 */
axios.interceptors.response.use(
  response => {
    return response
  },
  error => {
    if (error.response) {
      switch (error.response.status) {
        case 401:
        /** 401 清除token信息并跳转到登录页面 */
          console.log('----------------')
      }
    }
    /** console.log(JSON.stringify(error));//console : Error: Request failed with status code 402 */
    return Promise.reject(error.response.data)
  })

/** 公共的Ajax请求方法 */
export function ajaxPost (data, url) {
  let config = {
    headers: {'X-Requested-With': 'XMLHttpRequest', 'Content-Type': 'application/json', 'Accept': 'application/json'},
    responseType: 'json',
    method: 'post'
  }
  return axios.post(url, data, config).then((res) => {
    return Promise.resolve(res.data)
  }).catch(() => {
    return Promise.resolve(false)
  })
}
